package com.ltedu.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltedu.common.enums.ErrorCodeEnum;
import com.ltedu.common.validate.Create;
import com.ltedu.common.validate.Update;
import com.ltedu.common.vo.Result;
import com.ltedu.course.domain.Category;
import com.ltedu.course.domain.vo.CategoryVO;
import com.ltedu.course.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 类别表 前端控制器
 * </p>
 *
 * @author Jerry
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/course/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类树列表
     *
     * @return {@link Result}
     */
    @GetMapping("/tree")
    public Result tree() {
        return Result.ok(categoryService.treeList());
    }

    /**
     * 添加分类
     *
     * @param categoryVO
     * @return {@link Result}
     */
    @PostMapping("/add")
    public Result add(@Validated(Create.class) @RequestBody CategoryVO categoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVO, category);
        category.setGmtCreate(new Date());
        category.setGmtModified(new Date());
        return  Result.ok(categoryService.save(category));
    }

    /**
     * 更新分类
     *
     * @param categoryVO
     * @return {@link Result}
     */
    @PutMapping("/update")
    public Result update(@Validated(Update.class) @RequestBody CategoryVO categoryVO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryVO, category);
        category.setGmtModified(new Date());
        return  Result.ok(categoryService.updateById(category));
    }

    /**
     * 根据id获取分类信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.ok(categoryService.getById(id));
    }

    /**
     * 根据id删除分类
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        if (categoryService.hasChildById(id)) {
            return Result.fail(ErrorCodeEnum.EXIST_SUBCATEGORY.getMessage());
        }
        if (categoryService.checkCategoryExistCourse(id)) {
            return Result.fail(ErrorCodeEnum.CATEGORY_IS_USED.getMessage());
        }
        return Result.ok(categoryService.removeById(id));
    }

    /**
     * 获取一级分类
     *
     * @return {@link Result}
     */
    @GetMapping("/getOneLevelCategory")
    public Result getOneLevelCategory() {
        return Result.ok(categoryService.getOneLevelCategory());
    }

    /**
     * 获取二级分类
     *
     * @param parentId 父id
     * @return {@link Result}
     */
    @GetMapping("/getTwoLevelCategory/{parentId}")
    public Result getTwoLevelCategory(@PathVariable Long parentId) {
        return Result.ok(categoryService.getTwoLevelCategory(parentId));
    }
}

